package com.LachesismStorm.lachesism.member.controller;

//可能用到的包

import cn.hutool.core.lang.Assert;
import com.LachesismStorm.lachesism.core.common.response.ResponseResult;
import com.LachesismStorm.lachesism.core.util.CommonUtil;
import com.LachesismStorm.lachesism.core.util.LachesismTools;
import com.LachesismStorm.lachesism.member.service.OfficalAccountService;
import com.LachesismStorm.lachesism.member.vo.StringInfoVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author: lachesism
 * @Date: 2023/04/09/11:50
 * @Description:
 */
@RestController
@Api(tags = "公众号")
@RequestMapping("/officalAccount")
public class OfficalAccountController {

    @Resource
    private OfficalAccountService officalAccountService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 接口描述:
     * 测试公众号连接 params传参
     * 此方法校验通过
     */
    @RequestMapping(value = "/connect", method = RequestMethod.GET)
    public String handleGetRequest(@RequestParam String signature,
                                   @RequestParam String timestamp,
                                   @RequestParam Integer nonce,
                                   @RequestParam String echostr) {
        System.out.println("接收到微信公众号请求======================================");
        System.out.println("signature:  " + signature);
        System.out.println("timestamp:  " + timestamp);
        System.out.println("nonce:  " + nonce);
        System.out.println("echostr:  " + echostr);
        System.out.println("接收到微信公众号请求======================================");
        return echostr;
    }

    @RequestMapping(value = "/connect", method = RequestMethod.POST, produces = "application/xml;charset=UTF-8")
    public String handlePostRequest(HttpServletRequest request, HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        try {
            Map<String, String> map = LachesismTools.xmlToMap(request.getInputStream());
            officalAccountService.addMsgLog(map);
            Assert.notEmpty(map, "微信公众号事件回调xml数据包参数为空");
//            if ("event".equals(map.get("MsgType"))) {
//                String xmlBody = officalAccountService.replyEvent(map);
//                return xmlBody;
//            }
            if ("text".equals(map.get("MsgType"))) {
                System.out.println("接收到文字信息");
                return officalAccountService.replyText1(map);
            }
            if ("location".equals(map.get("MsgType"))) {
                System.out.println("接收到位置信息");
                return officalAccountService.replyLocation(map);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @ApiOperation(value = "获取token")
    @PostMapping("/getToken")
    public ResponseResult getToken() {
        String token = officalAccountService.getToken();
        return ResponseResult.success(token, "添加成功");
    }

    //    /**
//     * 接口描述:
//     * 获取access_token
//     * 此方法校验通过
//     */
//    @ApiOperation(value = "获取二维码地址")
//    @GetMapping("/getQcCode")
//    public ResponseResult getQcCode(@RequestParam String sceneStr) throws IOException {
//        LinkedList<String> res = officalAccountService.getQcCode(sceneStr);
//        return ResponseResult.success(res, "获取二维码地址成功");
//    }

//    @ApiOperation(value = "获取扫码结果")
//    @GetMapping("/getQcCodeResult")
//    public ResponseResult getQcCodeResult(@RequestParam String ticket) {
//        String opneId = (String) redisTemplate.opsForValue().get(ticket);
//        if (opneId != null) {
//            return ResponseResult.success(opneId, "获取扫码结果成功");
//        }
//        return ResponseResult.success("暂未扫码");
//    }

//    @ApiOperation(value = "获取配置的回复返回信息")
//    @GetMapping("/getReturnMessage")
//    public ResponseResult getReturnMessage() {
//        LinkedHashMap<String, String> returnMessage = officalAccountService.getReturnMessage();
//        return ResponseResult.success(returnMessage, "获取问题返回信息");
//    }

//    @GetMapping("/flushReturnMessage")
//    public void flushReturnMessage() {
//        officalAccountService.flushReturnMessage();
//    }
//
//    @GetMapping("/flushMenu")
//    public void flushMenu() throws IOException {
//        System.out.println("开始刷新前台菜单-MEMBER");
//        officalAccountService.flushMenu();
//    }
//
//    @PostMapping("/sendOrderPush")
//    public void sendOrderPush(@RequestBody OfaPushVO ofaPushVO) {
//        officalAccountService.sendOrderPush(ofaPushVO);
//    }
//
//    @PostMapping("/sendChargePush")
//    public void sendChargePush(@RequestBody OfaPushVO ofaPushVO) {
//        officalAccountService.sendChargePush(ofaPushVO);
//    }
//
//    @PostMapping("/sendConsumePush")
//    public void sendConsumePush(@RequestBody OfaPushVO ofaPushVO) {
//        officalAccountService.sendConsumePush(ofaPushVO);
//    }
//
//    @PostMapping("/sendInvoicePush")
//    public void sendInvoicePush(@RequestBody OfaPushVO ofaPushVO) {
//        officalAccountService.sendInvoicePush(ofaPushVO);
//    }
//
//    @PostMapping("/sendCustomizedRequest")
//    public void sendCustomizedRequest(@RequestBody OfaPushVO ofaPushVO) {
//        officalAccountService.sendCustomizedRequest(ofaPushVO);
//    }
//
//    @PostMapping("/convertImgAddress")
//    public void convertImgAddress() throws IOException {
//        officalAccountService.convertImgAddress();
//    }

//
//    @GetMapping("/userinfo")
//    public String getUserInfo(@RequestParam(name = "code", required = false) String code,
//                              @RequestParam(name = "state", required = false) String state) {
//        if (code == null) {
//            // 微信客户端打开网页
//            String redirectUrl = "http://www.biaoxiaozhu.com/userinfo";
//            String url = wxMpService.oauth2buildAuthorizationUrl(redirectUrl,
//                    WxConsts.OAuth2Scope.SNSAPI_USERINFO, state);
//            // 使用302跳转到微信授权页面
//            return "redirect:" + url;
//        } else {
//            // 用户同意授权后，微信会回调这个方法，并带上code和state参数
//            try {
//                WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(code);
//                WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(accessToken, null);
//                // TODO: 将用户信息记录到数据库或session中
//            } catch (WxErrorException e) {
//                // 处理异常
//            }
//            // 使用302跳转到其他页面
//            return "redirect:/";
//        }
//    }
//}


}
